//


import UIKit
import SnapKit

/**
*  三大块视图  
   1.原创视图
   2.转发视图
   3.toolBar
*/
//设置cell子控件的间距
let HomeCellMargin: CGFloat = 10

class CZStatusTableViewCell: UITableViewCell{
    
    //记录toolbar的顶部约束
    var toolbarTopYueShu: Constraint?
    
    //使用ViewModel模型
    var statusViewModel: CZStatusViewModel? {
        didSet {
         //给原创微博视图设置模型
            originalView.statusViewModle = statusViewModel
            
            //卸载约束
            toolbarTopYueShu?.uninstall()
            
//            retweetView.statuViewModel = statusViewModel
            //判断微博是否有转发对对象
            if statusViewModel?.status?.retweeted_status != nil {
                //有数据
                retweetView.statuViewModel = statusViewModel
                
                retweetView.hidden = false
                toolBarButtons.snp_makeConstraints(closure: { (make) -> Void in
                    //记录跟新的约束
                    self.toolbarTopYueShu = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            } else {
                //没有转发微博的情况
                retweetView.hidden = true
                
                toolBarButtons.snp_makeConstraints(closure: { (make) -> Void in
                    
                    //记录跟新约束
                    self.toolbarTopYueShu = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            
            
            toolBarButtons.statusViewModel = statusViewModel
            
        }
    }

    //懒加载添加原创视图
    private lazy var originalView: CZStatusOriginalView = CZStatusOriginalView()
    
    //懒加载转发微博视图
    private lazy var retweetView: CZStatusRetweetView = CZStatusRetweetView()
    
    //懒加载底部按钮视图
    private lazy var toolBarButtons: CZStatusRoolBar = CZStatusRoolBar()
    
    //样式
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //调用方法
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图设置约束
    private func setUpUI() {
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(toolBarButtons)
        contentView.addSubview(retweetView)
        
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        toolBarButtons.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(35)
        }
        
        //cell的约束 
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBarButtons)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
}


















